package com.xz.example;

import com.xz.example.model.dark_model.DarkAttachment;
import com.xz.example.model.dark_model.DarkWeeklReport;
import com.xz.example.model.simple_model.Attachment;
import com.xz.example.model.simple_model.WeeklyReport;
import com.xz.example.util.PrototypeManager;

import java.io.IOException;

/**
 * @author 张志航     
 * @version V1.0.0
 * @projectName design_mode
 * @title     Client   
 * @package    com.xz.example  
 * @date   2019/11/29 16:55  
 * @explain
 */
public class Client {

    /**
     * 需求:
     *      在使用某个办公系统时,有些岗位的员工发现他们每周的工作都大同小异,每次填写周报时很多内容都是重复的,为了提高工作周报的创建效率,大家
     *      希望能有一种机制创建相同或者相似的周报,包括创建周报的附件,试用原型模式解决该问题.
     * 分析:
     *      需要有周报的内容大致相同,然后又要每个周报是不同的对象,这就需要一份周报作为原型,其余的周报都可以克隆（深克隆）此原型。
     * @param args
     */
    public static void main(String[] args) throws Exception {

        // 浅克隆实现,修改下clone出的周报的附件,原型周报的附件也会更改,但是我们要求附件等属性也是可以更改的,所以需要进行深克隆
        //simpleClone();
        // 使用序列化从头实现深克隆
        //darkClone();
        // 测试下原型管理器
        testPrototypeManager();
    }

    private static void testPrototypeManager() throws IOException, ClassNotFoundException {
        PrototypeManager manager = new PrototypeManager();
        DarkWeeklReport darkWeeklReport = manager.get("default");
        System.out.println(darkWeeklReport);
    }

    public static void darkClone() throws IOException, ClassNotFoundException {
        DarkWeeklReport weeklyReport_prototype,weeklyReport_new;
        weeklyReport_prototype = new DarkWeeklReport();
        DarkAttachment attachment = new DarkAttachment();
        attachment.setName("原型周报附件.txt");
        weeklyReport_prototype.setAttachment(attachment);
        weeklyReport_prototype.setName("原型周报");
        weeklyReport_prototype.setDate("2019-12-2");
        weeklyReport_prototype.setContent("原型周报内容");
        weeklyReport_new = weeklyReport_prototype.deepClone();
        System.out.println("原型:"+weeklyReport_prototype);
        System.out.println("克隆:"+weeklyReport_new);
        // 修改下clone出的周报的附件,原型周报的附件也会更改
        weeklyReport_new.getAttachment().setName("克隆周报附件.doc");
        weeklyReport_prototype.getAttachment().download();
        System.out.println("是否是同一个周报对象:"+(weeklyReport_new == weeklyReport_prototype));
        System.out.println("是否是同一个附件对象:"+(weeklyReport_new.getAttachment() == weeklyReport_prototype.getAttachment()));

    }

    public static void simpleClone() throws CloneNotSupportedException {
        WeeklyReport weeklyReport_prototype,weeklyReport_new;
        weeklyReport_prototype = new WeeklyReport();
        Attachment attachment = new Attachment();
        attachment.setName("原型周报附件.txt");
        weeklyReport_prototype.setAttachment(attachment);
        weeklyReport_prototype.setName("原型周报");
        weeklyReport_prototype.setDate("2019-12-2");
        weeklyReport_prototype.setContent("原型周报内容");
        weeklyReport_new = weeklyReport_prototype.clone();
        System.out.println("原型:"+weeklyReport_prototype);
        System.out.println("克隆:"+weeklyReport_new);
        // 修改下clone出的周报的附件,原型周报的附件也会更改
        weeklyReport_new.getAttachment().setName("克隆周报附件.doc");
        weeklyReport_prototype.getAttachment().download();
        System.out.println("是否是同一个周报对象:"+(weeklyReport_new == weeklyReport_prototype));
        System.out.println("是否是同一个附件对象:"+(weeklyReport_new.getAttachment() == weeklyReport_prototype.getAttachment()));
    }

}
